package cn.laixueit.controller;

import cn.laixueit.base.R;
import cn.laixueit.pojo.Category;
import cn.laixueit.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author duxiang
 * @date 2023/12/29 15:12
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 查询指定父id的所有分类
     *
     * @param pid 父id 默认为0
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(required = true) String belongType,@RequestParam(defaultValue = "0") String pid,@RequestParam(required = false)String name){
        List<Category> list = categoryService.listCategory(belongType, pid,name);
        return R.ok().put("rows",list);
    }

    /**
     * 查询指定所属分类树形结构
     *
     * @param belongType 分类编号id
     * @return
     */
    @GetMapping("/treeList")
    public R listTree(@RequestParam(required = false) String belongType){
        List<Map<String, Object>> tree = categoryService.listTree(belongType);
        return R.ok().put("tree",tree);
    }

    @PostMapping("/save")
    public R save(@RequestBody Category category){
        categoryService.saveCategory(category);
        return R.ok();
    }

    @DeleteMapping("/removeCategory/{id}")
    public R removeCategory(@PathVariable Integer id){
        categoryService.removeCategory(id);
        return R.ok();
    }
}
